package com.example.text.service.serviceImpl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.text.dao.AdminMenuDao;
import com.example.text.entity.AdminMenu;
import com.example.text.entity.AdminRoleMenu;
import com.example.text.entity.AdminUserRole;
import com.example.text.service.AdminMenuService;
import com.example.text.service.AdminRoleMenuService;
import com.example.text.service.AdminUserRoleService;
import com.example.text.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AdminMenuServiceImpl extends ServiceImpl<AdminMenuDao, AdminMenu> implements AdminMenuService {


    @Autowired
    AdminMenuDao adminMenuDao;

    @Autowired
    UserService userService;
    @Autowired
    AdminUserRoleService adminUserRoleService;

    @Autowired
    AdminRoleMenuService adminRoleMenuService;

    public List<AdminMenu> getAllByParentId(int parentId,List<Integer> ids) {

        return adminMenuDao.getAllByParentId(parentId,ids);
    }

    public List<AdminMenu> getMenusByCurrentUser() {
        // 从数据库中获取当前用户
        String username = SecurityUtils.getSubject().getPrincipal().toString();
        //System.out.println(username);

        //UserEntity userEntity=userService.getByUserName(username);

        // 根据uid,获得当前用户对应的所有角色规则的 ird 列表
        List<Integer> rids = adminUserRoleService.listAllByUid(username)
                .stream().map(AdminUserRole::getRid).collect(Collectors.toList());
        //ystem.out.println(rids);
        // 查询出这些角色对应的所有菜单项
        List<Integer> menuIds = adminRoleMenuService.findAllByRid(rids)
                .stream().map(AdminRoleMenu::getMid).collect(Collectors.toList());
        //System.out.println(menuIds);

        //根据菜单id,查询对应菜单属性
        List<AdminMenu> menus = adminMenuDao.findAllListByMenuIds(menuIds).stream().distinct().collect(Collectors.toList());

        // 处理菜单项的结构
        handleMenus(menus);
        return menus;
    }

    public void handleMenus(List<AdminMenu> menus) {

         List<Integer> ids=new ArrayList<>();

         //获取对应传入存在所有菜单的id
        menus.forEach(m -> {
            ids.add(m.getId());
        });
        menus.forEach(m -> {
            //查询ParentId=AdminMenu.id，且在满足传入menus的ids值 避免查询子菜单，如果传入有三个子菜单，但是查询出来的数据库是有四个子菜单
            List<AdminMenu> children = getAllByParentId(m.getId(),ids);
            m.setChildren(children);
        });

        menus.removeIf(m -> m.getParent_id() != 0);
    }

    public List<AdminMenu> getMenusByRoleId(int rid) {
        List<Integer> menuIds=adminRoleMenuService.findAllByRid(rid)
                .stream().map(AdminRoleMenu::getMid).collect(Collectors.toList());

        System.out.println("getMenusByRoleId:menuIds="+menuIds);
        if(!menuIds.isEmpty()) {
            List<AdminMenu> menus = adminMenuDao.findAllListByMenuIds(menuIds);
            handleMenus(menus);
            return menus;
        }

        return null;


    }


}
